import axios from 'axios'
import { getBaseUrl } from './config'
import { getCookie } from './cookie'
import ls from './localStorage'
import { SD_TOKEN } from '../common/keys'

// console.log(getBaseUrl)
axios.defaults.baseURL = getBaseUrl;
// axios.defaults.headers.post['Content-Type'] = "application/json";
axios.defaults.headers.put['Content-Type'] = "application/json";
axios.defaults.headers.patch['Content-Type'] = "application/json";


// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    let token = getCookie(SD_TOKEN)
    // console.log(token)
    // 非登录接口
    if (config.url.indexOf('login') === -1 && token == undefined) {
        ls.clearAll();
        location.href = '/#/login';
        location.reload();
    }

    //非登录接口携带token
    if (config.url.indexOf('login') === -1) {
        config.headers.common['Authorization'] = `Bearer ${token}`;
    }
 return Promise.resolve(config);
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// // 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // console.log(response)
    // 对响应数据做点什么
    return Promise.resolve(response.data);
}, function (error) {
    if (error.response.status == 401) {
        ls.clearAll();
        location.href = '/#/login';
        location.reload();
    }
    // 对响应错误做点什么
    return Promise.reject(error);
});

export default class SdAxios{
    constructor(options = {}) {
        this.options = options
    }

    request(options) {
        return axios.request(options);
    }

    get(url, options = {}) {
        return this.reuqest({
            url,
            params: {
                ...options
            }
        })
    }

    delete(url, options = {}) {
        return this.request({
            methods: 'delete',
            url,
            params: {
                ...options
            }
        })
    }

    post(url, data, options = {}) {
        // if (data instanceof Object) {
        //     data = JSON.stringify(data)
        // }
        // return this.reuqest({
        //     methods: 'post',
        //     url,
        //     data,
        //     ...options
        // })
        // if (data instanceof Object) {
        //     data = JSON.stringify(data);
        // }
        return this.request({
            method: 'post',
            url,
            data,
            ...options
        });
    }

    put(url, data, options = {}) {
        if (data instanceof Object) {
            data = JSON.stringify(data)
        }
        return this.reuqest({
            methods: 'put',
            url,
            data,
            ...options
        })
    }

    patch(url, data, options = {}) {
        if (data instanceof Object) {
            data = JSON.stringify(data)
        }
        return this.reuqest({
            methods: 'put',
            url,
            data,
            ...options
        })
    }
}

const instanceAxios = new SdAxios;
export const request = instanceAxios.request.bind(instanceAxios);
export const get = instanceAxios.get.bind(instanceAxios);
export const post = instanceAxios.post.bind(instanceAxios);
export const put = instanceAxios.put.bind(instanceAxios);
export const patch = instanceAxios.patch.bind(instanceAxios);
export const zDelete = instanceAxios.delete.bind(instanceAxios);